#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>

void myls_l(char *pathname,char *app_pathname);

int main(int argc, char *argv[])
{
	char buf[1024] = "";												//路径名
	//
	if(argc == 1)														//没有输入其他参数时，打开当前目录
	{
		getcwd(buf, sizeof(buf));	//获得当前路径
		char app_path[512];
		sprintf(app_path,"%s/%s",buf,"mycp_hbb");		//存放mycp程序的路径		
		myls_l(buf,app_path);
	}
	else if(argc > 2){
		printf("error, too many arguments\n");
		return 0;
	}	
	else															//输入要展示的目录
	{	
		getcwd(buf, sizeof(buf));
		char app_path[512];
		sprintf(app_path,"%s/%s",buf,"mycp_hbb");                       //存放mycp程序的路径
		// printf("%s\n", buf);
		myls_l(argv[1],app_path);
	}
	return 0;
}

void myls_l(char *pathname,char *app_pathname)
{
	DIR *cwd = NULL;													//路径文件指针
	struct dirent *det = NULL;											//记录读到的文件信息
	struct stat statbuf = {0};											//记录文件的属性
	struct passwd *pwd = NULL;											//记录文件所属用户
	struct group *grp = NULL;											//记录文件所属用户组
	struct tm mtm = {0};
	
	char path[512];
	chdir(pathname);													//!非常重要，否则无法获取文件属性
	//printf("%s\n", buf);												//输出当前路径
	cwd = opendir(pathname);											//打开目录
	//printf("pathname: %s \n",pathname);
	while(NULL != (det = readdir(cwd)))									//遍历当前目录
	{	
		//printf("pathname: %s \n",pathname);
		if(strncmp(det->d_name, ".", 1))
		{	
			sprintf(path,"%s/%s",pathname,det->d_name);                 //记录路径
			printf("path: %s\n",path); 
			
			stat(path, &statbuf);


			if(!S_ISDIR(statbuf.st_mode)){
				pid_t pid_child , pid_return;
				pid_child = fork();

				if(pid_child == -1)
					printf("fork error");
				else if (pid_child==0)
				{
					execl(app_pathname,"mycp_hbb",path,"/home/hbb/kkxixi",NULL);
					//myls_l(det->d_name);
					exit(0);
					
				}

				do
					{
						pid_return=waitpid(pid_child, NULL, WNOHANG);//pid_child这里返回的是子进程的进程ID
						if (pid_return==0)
						{
							//printf("No child exited\n");
							sleep(1);
						}
					}while(pid_return==0);

				// if (pid_return==pid_child)
				// 	printf("successfully get child %d\n", pid_return);
				// else 
				// 	printf("some error occured\n");
				
			}else{
				myls_l(path,app_pathname);
			}
	
			
			//st_mode													//文件类型
			if(S_ISREG(statbuf.st_mode))
				printf("-");
			else if(S_ISDIR(statbuf.st_mode))
				printf("d");
			else if(S_ISCHR(statbuf.st_mode))
				printf("c");
			else if(S_ISBLK(statbuf.st_mode))
				printf("b");
			else if(S_ISFIFO(statbuf.st_mode))
				printf("p");
			else if(S_ISLNK(statbuf.st_mode))
				printf("l");
			else if(S_ISSOCK(statbuf.st_mode))
				printf("s");
			//st_mode													//文件权限
			if(statbuf.st_mode & S_IRUSR)
				printf("r");
			else
				printf("-");
			if(statbuf.st_mode & S_IWUSR)
				printf("w");
			else
				printf("-");
			if(statbuf.st_mode & S_IXUSR)
				printf("x");
			else
				printf("-");

			if(statbuf.st_mode & S_IRGRP)
				printf("r");
			else
				printf("-");
			if(statbuf.st_mode & S_IWGRP)
				printf("w");
			else
				printf("-");
			if(statbuf.st_mode & S_IXGRP)
				printf("x");
			else
				printf("-");

			if(statbuf.st_mode & S_IROTH)
				printf("r");
			else
				printf("-");
			if(statbuf.st_mode & S_IWOTH)
				printf("w");
			else			
				printf("-");
			if(statbuf.st_mode & S_IXOTH)
				printf("x");
			else
				printf("-");
			//st_nlink													//硬链接数
			printf("%2ld ", statbuf.st_nlink);
			//st_uid													//属主
			pwd = getpwuid(statbuf.st_uid);
			printf("%s ", pwd->pw_name);
			//st_gid													//属组
			grp = getgrgid(statbuf.st_gid);
			printf("%s ", grp->gr_name);
			//st_size													//文件大小
			printf("%5ld ", statbuf.st_size);
			//st_mtim													//最后修改时间
			localtime_r(&(statbuf.st_mtime), &mtm);
			printf("%d-", mtm.tm_year+1900);
			printf("%d-", mtm.tm_mon + 1);
			printf("%d ", mtm.tm_mday);
			printf("%02d:", mtm.tm_hour);
			printf("%02d ", mtm.tm_min);
			//st_name													//文件名
			printf("%s\n", det->d_name);
		}
	}
	closedir(cwd);														//关闭目录
}